<template>
  <el-dialog
    model-value="dialogVisible"
    :title="dialogTitle"
    :parentMenuList="parentMenuList"
    width="60%"
    @close="handleClose"
  >
    <el-form :model="form" label-width="120px" ref="formRef" :rules="rules">
      <el-form-item label="权限编号" prop="permNo">
        <el-input v-model="form.permNo" />
      </el-form-item>
      <el-form-item label="权限名称" prop="permName">
        <el-input v-model="form.permName" />
      </el-form-item>
      <el-form-item label="请求路径" prop="permUrl">
        <el-input v-model="form.permUrl" />
      </el-form-item>
      <el-form-item label="权限标识" prop="permLabel">
        <el-input v-model="form.permLabel" />
      </el-form-item>
      <el-form-item label="父级编号" prop="parentNo">
        <el-cascader
          v-model="form.parentNo"
          placeholder="可搜索"
          :options="parentMenuList"
          :props="menuProps"
          filterable
          clearable
        />
      </el-form-item>
      <el-form-item label="权限类型" prop="permType">
        <el-select v-model="form.permType" placeholder="请选择">
          <el-option
            v-for="item in permTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="显示级别" prop="showLevel">
        <el-select v-model="form.showLevel" placeholder="请选择">
          <el-option
            v-for="item in showLevelList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序号" prop="orderNo">
        <el-input v-model="form.orderNo" />
      </el-form-item>
      <el-form-item label="公共标识" prop="publicFlag">
        <el-select v-model="form.publicFlag" placeholder="请选择">
          <el-option
            v-for="item in publicFlagList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { addPerm, editPerm } from '@/api/perm'
import { ElMessage } from 'element-plus'
import { showLevelList, permTypeList, publicFlagList } from '@/util/dict'
const emits = defineEmits([
  'update:modelValue',
  'initMenuList',
  'initParentMenuList'
])
const formRef = ref(null)
const form = ref({
  permNo: '',
  permName: '',
  permUrl: '',
  parentNo: '',
  orderNo: '',
  permLabel: '',
  permType: '',
  showLevel: '',
  publicFlag: ''
})

const props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false,
    required: false
  },
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  dialogTableVal: {
    type: Object,
    // default: {} 此处会报错 因为vue规定，对象或数组默认值必须从一个工厂函数获取
    default: function () {
      return {}
    }
  },
  parentMenuList: {
    type: Array,
    default: function () {
      return []
    }
  }
})

watch(
  () => props.dialogTableVal,
  () => {
    form.value = props.dialogTableVal
  },
  { deep: true, immediate: true }
)

const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      if (form.value.parentNo) {
        let arr = Object.values(form.value.parentNo)
        form.value.parentNo = arr[arr.length - 1]
      } else {
        form.value.parentNo = -1
      }
      props.dialogTitle === '添加权限'
        ? await addPerm(form.value)
        : await editPerm(form.value)
      ElMessage({
        message: '成功',
        type: 'success'
      })
      emits('initMenuList')
      emits('initParentMenuList')
      handleClose()
    } else {
      ElMessage({
        message: '提交失败!!',
        type: 'warning'
      })
      return false
    }
  })
}

const rules = ref({
  permNo: [
    {
      required: true,
      message: '请填写权限编号',
      trigger: 'blur'
    }
  ],
  permName: [
    {
      required: true,
      message: '请填写权限名称',
      trigger: 'blur'
    }
  ],
  permUrl: [
    {
      required: true,
      message: '请填写请求路径',
      trigger: 'blur'
    }
  ],
  permType: [
    {
      required: true,
      message: '请选择权限类型',
      trigger: 'blur'
    }
  ],
  showLevel: [
    {
      required: true,
      message: '请选择显示级别',
      trigger: 'blur'
    }
  ],
  publicFlag: [
    {
      required: true,
      message: '请选择公共标识',
      trigger: 'blur'
    }
  ],
  permLabel: [
    {
      required: true,
      message: '请填写权限标识',
      trigger: 'blur'
    }
  ]
})

const menuProps = {
  checkStrictly: true
}

const handleClose = () => {
  // 父组件 v-model 没有指定参数名，则默认是 modelValue
  emits('update:modelValue', false)
}
</script>

<style lang="scss" scoped></style>
